package com.fare.taluo.web.action;

import java.io.File;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.crawl.TaskAdapterApplication;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.vo.adapter.AdapterVO;
import com.fare.taluo.vo.core.SourceEntityVO;


@Controller("adaptorAction")
@Scope("prototype")
public class AdapterAction extends BaseFdcAction{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8861990214567884185L;
	private List<SourceEntityVO> sources;
	@Resource(name="sourceApplication")
	private SourceApplication sourceApplication;
	private File adapter;
	private String adapterFileName;
	private String adapterContentType;
	private AdapterVO vo;
	private Object result;
	private Long sourceID;
	private Long id;
	private Integer isEnable;
	private String adapterClassName;
	private Long adapterFileId;
	
	@Autowired
	private TaskAdapterApplication taskAdapterApplication;
	
	public String toAdaptors() throws Exception {
		returnPageURL = "/fare/_zqspqsz.jsp";
		this.sources=sourceApplication.findAllSourceEntity();
		return "dispatcher";
	}
	public String toUpAdapterJsp()throws Exception{
		returnPageURL = "/fare/upAdapter.jsp";
		this.sources=sourceApplication.findAllSourceEntity();
		return "dispatcher";
	}
	
	/**
	 * 上传class文件
	 * @return
	 * @throws Exception
	 */
	public String upload()throws Exception{
		vo.setAdapterFile(adapter);
		vo.setAdapterClassName(adapterFileName);
		try {
			taskAdapterApplication.uploadAdapter(vo);
			result="success";
		} catch (Exception e) {
			result="fail";
		}
		returnPageURL = "/fare/upAdapter.jsp";
		this.sources=sourceApplication.findAllSourceEntity();
		return "dispatcher";
	}
	/**
	 * 获取列表
	 * @throws Exception
	 */
	public void getList()throws Exception{
		List<AdapterVO> vos=taskAdapterApplication.findAdapterByConsdions(sourceID,isEnable,adapterClassName);
		super.toJSONOut(com.fare.taluo.util.JsonUtil.toJson(vos));
	}
	
	
	public void closeOrOpenAdapter()throws Exception{
		taskAdapterApplication.closeOrOpenAdapter(id, isEnable);
		result="success";
		super.toJSONOut(result.toString());
	}
	
	
	public void deleteAdapter()throws Exception{
		taskAdapterApplication.deleteAdapter(id);
		result="success";
		super.toJSONOut(result.toString());
	}
	
	/**
	 * 下载适配器文件
	 * @throws Exception
	 */
	public void downAdapter()throws Exception{
		InputStream is=null;
		ServletOutputStream out = null;
		String fileName;
		byte[] b;
		try{
			vo=taskAdapterApplication.downloadAdapterFileById(adapterFileId);
			fileName=vo.getAdapterClassName();
			is=vo.getBlob().getBinaryStream();
			out = response.getOutputStream();
			response.reset();
			// 设置输出的格式
			response.setContentType("application/pdf;charset=UTF-8");
			if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0){
				fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");//firefox浏览器
			}else{
				fileName = URLEncoder.encode(fileName, "UTF-8");//其他浏览器
			}
			response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
			// 循环取出流中的数据
			b = new byte[1024*1024];
			int len;
			while ((len = is.read(b)) > 0){
				out.write(b, 0, len);
			}
			out.flush();
		}finally{
			if(out!=null){out.close();out=null;}
			if(is!=null){is.close();is=null;}
			fileName=null;
			b=null;
		}
	}
	
	public List<SourceEntityVO> getSources() {
		return sources;
	}
	public void setSources(List<SourceEntityVO> sources) {
		this.sources = sources;
	}
	public SourceApplication getSourceApplication() {
		return sourceApplication;
	}
	public void setSourceApplication(SourceApplication sourceApplication) {
		this.sourceApplication = sourceApplication;
	}
	public File getAdapter() {
		return adapter;
	}
	public void setAdapter(File adapter) {
		this.adapter = adapter;
	}
	public String getAdapterFileName() {
		return adapterFileName;
	}
	public void setAdapterFileName(String adapterFileName) {
		this.adapterFileName = adapterFileName;
	}
	public String getAdapterContentType() {
		return adapterContentType;
	}
	public void setAdapterContentType(String adapterContentType) {
		this.adapterContentType = adapterContentType;
	}
	
	public AdapterVO getVo() {
		return vo;
	}
	public void setVo(AdapterVO vo) {
		this.vo = vo;
	}
	public TaskAdapterApplication getTaskAdapterApplication() {
		return taskAdapterApplication;
	}
	public void setTaskAdapterApplication(
			TaskAdapterApplication taskAdapterApplication) {
		this.taskAdapterApplication = taskAdapterApplication;
	}
	public Object getResult() {
		return result;
	}
	public void setResult(Object result) {
		this.result = result;
	}
	public Long getSourceID() {
		return sourceID;
	}
	public void setSourceID(Long sourceID) {
		this.sourceID = sourceID;
	}
	public Integer getIsEnable() {
		return isEnable;
	}
	public void setIsEnable(Integer isEnable) {
		this.isEnable = isEnable;
	}
	public String getAdapterClassName() {
		return adapterClassName;
	}
	public void setAdapterClassName(String adapterClassName) {
		this.adapterClassName = adapterClassName;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getAdapterFileId() {
		return adapterFileId;
	}
	public void setAdapterFileId(Long adapterFileId) {
		this.adapterFileId = adapterFileId;
	}
	

	
	
}
